//
//  CAImageBrowser.m
//  cascade-realm
//
//  Created by QSC on 1/6/24.
//

#import "CAImageBrowser.h"
@interface CAImageBrowser()
@property(nonatomic,strong)NSArray * images;
@property(nonatomic,assign)NSInteger index;
@property(nonatomic,strong)UIImageView * topImagev;
@property(nonatomic,strong)UIView * imageContent;
@end

@implementation CAImageBrowser

+ (void)ShowWithImages:(NSArray *)images index:(NSInteger)index{
    
    CAImageBrowser * alert = [CAImageBrowser new];
    alert.frame = [UIScreen mainScreen].bounds;
    alert.images = images;
    alert.index = index;
    [[UIApplication sharedApplication].keyWindow addSubview:alert];
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        [self setUpView];
        WeakSelf
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(UITapGestureRecognizer * _Nonnull sender) {
            
            CGPoint p = [sender locationInView:weakSelf.topImagev];
            BOOL result = [weakSelf.topImagev.layer containsPoint:p];
            if (!result) {
                [weakSelf removeFromSuperview];
            }


        }];
        
        [self addGestureRecognizer:  tap];
    }
    return self;
}

- (void)setUpView{
    
    UIImageView *topImgv = [UIImageView new];
    topImgv.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:topImgv];
    topImgv.clipsToBounds = YES;
    topImgv.layer.cornerRadius =  8;
    topImgv.userInteractionEnabled = YES;
    self.topImagev = topImgv;
    [topImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.left.equalTo(self).offset(24);
        make.top.equalTo(self).offset(kNavigationBarHeight + 15);
        if (IsX) {
            make.height.mas_equalTo(ScreenW * 510/375);
        }else{
            make.height.mas_equalTo(ScreenW * 400/375);
        }
        
    }];
    
    UIView * content = [UIView new];
    [self addSubview:content];
    self.imageContent = content;
    [content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topImgv.mas_bottom).offset(24);
        make.centerX.equalTo(self);
        make.height.mas_equalTo(80);
        make.left.equalTo(self).offset(38);
    }];
}


- (void)setIndex:(NSInteger)index{
    _index = index;
    [self reload];
}


- (void)reload{
    self.topImagev.image = Img(self.images[self.index]);
    
    [self.imageContent removeAllSubviews];
    
    
    CGFloat W = 65;
    CGFloat H = 80;
    NSInteger count = MIN(self.images.count, 4);
    CGFloat margin = (ScreenW - 38 * 2 - self.images.count * 65)/ (self.images.count + 1);
    
    for (int i = 0; i < count; i++) {
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundImage:Img(self.images[i]) forState:UIControlStateNormal];
        [self.imageContent addSubview:btn];
        
        btn.frame = CGRectMake(margin + i * (margin + W), 0, W, H);
        btn.layer.cornerRadius = 4;
        btn.clipsToBounds = true;
        if (i == self.index) {
            btn.layer.borderWidth = 1;
            btn.layer.borderColor = Hex(0xD4E600).CGColor;
        }
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)btnClick:(UIButton *)btn{
    self.index = btn.tag;
}
@end
